// pages/main/sales/connections.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        users: [],
        url: getApp().globalData.apiurl,

        source: 0,
        loadingType: "more",
        totalCount: 0,
        pageIndex: 1,
        listData: [],
        formlist: {},
        pageSize: 10,
        currentPage: 1,
        total_count: 0, //总条数
        scrollHeight: 0,
        pageNum: 0,
        listName: ""

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
        let n = this;
        n.setData({
            listName:getApp().globalData.userinfo.nickName
        })
        console.log(n.data.listName)
        // console.log(n.data.roles)
        if(getApp().globalData.userinfo.roles[0].roleKey == 'admin'){
            wx.request({
                url: n.data.url + "customer/customer/list",
                method: "GET",
                data: {
                    isdelete: 5,
                    pageNum: n.data.currentPage,
                    pageSize: n.data.pageSize
                },
                success: function (t) {
                    n.setData({
                        users: t.data.rows,
                        currentPage: n.data.currentPage + 1,
                        pageNum: t.data.total / n.data.pageSize + 1
                    })
                    console.log(t)
                    //  console.log(n.users)
                },
                fail: function () {
                    //   console.log("请求失败")
                },
                complete: function () {
                    //   console.log("请求完成")
                }
            });
        }else{
            wx.request({
                url: n.data.url + "customer/customer/list",
                method: "GET",
                data: {
                    isdelete: 5,
                    pageNum: n.data.currentPage,
                    pageSize: n.data.pageSize,
                    createordno: getApp().globalData.userinfo.nickName,
                    roles: getApp().globalData.userinfo.roles[0].roleKey
                },
                success: function (t) {
                    n.setData({
                        users: t.data.rows,
                        currentPage: n.data.currentPage + 1,
                        pageNum: t.data.total / n.data.pageSize + 1
                    })
                    console.log(t)
                    //  console.log(n.users)
                },
                fail: function () {
                    //   console.log("请求失败")
                },
                complete: function () {
                    //   console.log("请求完成")
                }
            });
        }
        
        wx.getSystemInfo({
            success: function (res) {
                n.setData({
                    scrollHeight: res.windowHeight - 30
                })
            }
        });
    },
    getList: function (e) {

        let fromdatas = "";
        //  console.log(e)

        if (e != undefined)
            if (e.detail != undefined && e.detail.value.customername != undefined) {
                fromdatas = e.detail.value.customername;
            }

        // fromdatas.id = this.data.form.id;


        let n = this;
        if(getApp().globalData.userinfo.roles[0].roleKey == 'admin'){
            wx.request({
                url: n.data.url + "customer/customer/list",
                method: "GET",
                data: {
                    pageNum: n.data.currentPage,
                    pageSize: n.data.pageSize,
                    customername: fromdatas
                },
                success: function (t) {
                    // console.log("请求失败")
                    // console.log(t)
                    n.setData({
                        users: t.data.rows,
                        currentPage: 2,
                        pageNum: t.data.total / n.data.pageSize + 1
                    })
    
                    //   console.log(n.users)
                },
                fail: function () {
                    //   console.log("请求失败")
                },
                complete: function () {
                    //   console.log("请求完成")
                }
            });
        }else{
            wx.request({
                url: n.data.url + "customer/customer/list",
                method: "GET",
                data: {
                    pageNum: n.data.currentPage,
                    pageSize: n.data.pageSize,
                    customername: fromdatas,
                    createordno: getApp().globalData.userinfo.nickName,
                    roles: getApp().globalData.userinfo.roles[0].roleKey
                },
                success: function (t) {
                    // console.log("请求失败")
                    // console.log(t)
                    n.setData({
                        users: t.data.rows,
                        currentPage: 2,
                        pageNum: t.data.total / n.data.pageSize + 1
                    })
    
                    //   console.log(n.users)
                },
                fail: function () {
                    //   console.log("请求失败")
                },
                complete: function () {
                    //   console.log("请求完成")
                }
            });
        }
        
    },
    myclick: function () {
        // var pages = getCurrentPages(); //当前页面
        // var beforePage = pages[pages.length - 2]; //前一页
        // beforePage.onLoad(); // 执行前一个页面的onLoad方法
        // wx.navigateBack({
        //     url:"/pages/main/sales/addconnections",
        //   delta: 1
        // });

        wx.redirectTo({
            url: "/pages/main/sales/addconnections"
        })
    },
    reviseclick: function (e) {
        let _that = this;
        let cuid = e.currentTarget.dataset.connectionid.split(",");
        if (cuid[1] == getApp().globalData.userinfo.nickName) {
            wx.redirectTo({
                url: "/pages/main/sales/reviseconnection?connectionid=" + cuid[0]
            })
        }
    },
    // 新增按钮，点击后跳转到新增客户页面
    submitForm: function () {
        wx.navigateTo({
            url: ""
        });
        //  console.log("S")
        var e = this;
        this.listData = [];
        // t.getStorageSync("key"), getApp().globalData.userinfo.roles[0].roleKey, getApp().globalData.userinfo.nickName;
        wx.request({
            url: e.url + "customer/customer/list",
            method: "GET",
            success: function (t) {
                //        console.log(t.data.rows)
                e.setData({
                    listData: t.data.rows
                })

            },
            fail: function () {},
            complete: function () {}
        })
    },
    del: function (e) {

        let subIds = [];
        subIds.push(e.target.dataset.ids.id)
        //确认框
        wx.showModal({
            title: '友情提示',
            content: '确认删除  ' + e.target.dataset.ids.customername + '  数据吗？',
            success: ({
                confirm
            }) => {
                if (confirm) {
                    // 调用删除接口
                    this.gotodel(subIds)
                }
            }
        })
    },

    gotodel: function (subIds) {

        let n = this;
        wx.request({
            url: n.data.url + "customer/customer/" + subIds,
            method: 'DELETE',
            success: res => {
                wx.showToast({
                    title: '删除成功',
                    //提示的延迟时间
                    duration: 1000
                })
            },
            fail: err => {
                wx.showToast({
                    title: '删除失败',
                    //提示的延迟时间
                    duration: 1000
                })
            },
            complete: end => {
                this.getList();
            }
        })
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    loadMoreData() {
        // 请求新数据
        let n = this;
        console.log(n.data.users)
        if (n.data.currentPage < n.data.pageNum) {
            if(getApp().globalData.userinfo.roles[0].roleKey == 'admin'){
                wx.request({
                    url: n.data.url + "customer/customer/list",
                    method: "GET",
                    data: {
                        isdelete: 5,
                        pageNum: n.data.currentPage,
                        pageSize: n.data.pageSize
                    },
                    success: function (t) {
                        console.log(t.data)
                        t.data.rows.forEach(item => {
                            n.data.users.push(item)
                        });
                        // t.listData = e.data.rows,
                        n.setData({
                            users: n.data.users,
                            currentPage: n.data.currentPage + 1
                        })
                        //  console.log(n.users)
                    },
                    fail: function () {
                        //   console.log("请求失败")
                    },
                    complete: function () {
                        //   console.log("请求完成")
                    }
                });
            }else{
                wx.request({
                    url: n.data.url + "customer/customer/list",
                    method: "GET",
                    data: {
                        isdelete: 5,
                        pageNum: n.data.currentPage,
                        pageSize: n.data.pageSize,
                        createordno: getApp().globalData.userinfo.nickName,
                        roles: getApp().globalData.userinfo.roles[0].roleKey
                    },
                    success: function (t) {
                        console.log(t.data)
                        t.data.rows.forEach(item => {
                            n.data.users.push(item)
                        });
                        // t.listData = e.data.rows,
                        n.setData({
                            users: n.data.users,
                            currentPage: n.data.currentPage + 1
                        })
                        //  console.log(n.users)
                    },
                    fail: function () {
                        //   console.log("请求失败")
                    },
                    complete: function () {
                        //   console.log("请求完成")
                    }
                });
            }
            
        }
        //let n = this;


        // wx.request({
        //     url: n.data.url + "customer/customer/list",
        //     method: "GET",
        //     data: {
        //       page: n.data.page + 1,
        //       pageSize: n.data.pageSize
        //     },
        //     success: res => {
        //       // 将新数据添加到已有数据中
        //       const newListData = n.data.listData.concat(listData);
        //       // 更新页面数据
        //       n.setData({
        //         listData: newListData,
        //         page: n.data.page + 1
        //       });
        //     }
        //   })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})